package com.three.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.three.entity.BranchGoods;
import com.three.entity.Outlibrarytransfer;
import com.three.entity.OutlibrarytransferGoods;
import com.three.entity.Staff;
import com.three.mapper.BranchGoodsMapper;
import com.three.mapper.OutlibraryGoodsMapper;
import com.three.mapper.OutlibrarytransferGoodsMapper;
import com.three.mapper.OutlibrarytransferMapper;
import com.three.service.OutLibraryTransferService;
import com.three.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Service
public class OutLibraryTransferServiceImpl implements OutLibraryTransferService {
    @Autowired
    private OutlibrarytransferMapper outlibrarytransferMapper;
    @Autowired
    private OutlibrarytransferGoodsMapper outlibrarytransferGoodsMapper;
    @Autowired
    private HttpSession session;
    @Autowired
    private BranchGoodsMapper branchGoodsMapper;
    @Override
    public R getOutLibraryTransfer(Integer currentPage, Integer pageSize) {
        //开启分页
        PageHelper.startPage(currentPage,pageSize);
        //根据登录者店铺id查询平调审核单
        Staff staff = (Staff) session.getAttribute("staff");
        List<Outlibrarytransfer> outlibrarytransfers = outlibrarytransferMapper.getOutLibraryTransfer(staff.getBranchid());
        //封装分页数据
        PageInfo<Outlibrarytransfer> pageInfo = new PageInfo<>(outlibrarytransfers);
        return R.ok(pageInfo);
    }

    @Override
    public R getTransferInformation(Integer currentPage, Integer pageSize, TransferInformationVo transferInformationVo) {
        //开启分页
        PageHelper.startPage(currentPage,pageSize);
        List<Outlibrarytransfer> outlibrarytransfers = outlibrarytransferMapper.getTransferInformation(transferInformationVo);
        //封装分页数据
        PageInfo<Outlibrarytransfer> pageInfo = new PageInfo<>(outlibrarytransfers);
        return R.ok(pageInfo);
    }

    @Override
    public R getOutTransferGoodsByCid(Integer cid,Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<OutlibrarytransferGoods> outlibrarytransferGoods = outlibrarytransferGoodsMapper.getOutTransferGoodsByCid(cid);
        PageInfo<OutlibrarytransferGoods> pageInfo = new PageInfo<>(outlibrarytransferGoods);
        return R.ok(pageInfo);
    }

    @Override
    public R flattened(Integer flattenedBranchId, List<OutTransferListVo> outTransferListVoList) {
        //1.添加平调单
        //赋值
        Outlibrarytransfer outlibrarytransfer = new Outlibrarytransfer();
        outlibrarytransfer.setNewbranchid(flattenedBranchId);//新店铺id
        Staff staff = (Staff) session.getAttribute("staff");
        outlibrarytransfer.setOutstaffid(staff.getStaffid());//出库人的id
        outlibrarytransfer.setOuttime(new Date());//出库时间
        outlibrarytransfer.setOutmethod(3);//出库方式为平调
        outlibrarytransfer.setExamstate(1);//审核状态为未审核
        outlibrarytransfer.setBranchid(staff.getBranchid());//原始分店
        //进行添加
        int i = outlibrarytransferMapper.insertSelective(outlibrarytransfer);
        if (i>0){
            for (OutTransferListVo outTransferListVo : outTransferListVoList) {
                //为outtransferGoods赋值，添加平调商品
                OutTransferVo outTransferVo = new OutTransferVo();
                outTransferVo.setCid(outlibrarytransfer.getCid());
                outTransferVo.setGoodsid(outTransferListVo.getGoodsid());
                outTransferVo.setNum(outTransferListVo.getChuku());
                outTransferVo.setSprice(outTransferListVo.getSprice());
                outTransferVo.setTotal(outTransferListVo.getSprice()*outTransferListVo.getChuku());
                int j = outlibrarytransferGoodsMapper.insertOutTransferGoods(outTransferVo);
            }
        }
        return R.ok();
    }

    @Override
    @Transactional
    //通过审核
    public R pass(Integer cid) {
        //根据cid查询出库单
        Outlibrarytransfer outlibrarytransfer = outlibrarytransferMapper.selectByPrimaryKey(cid);
        Staff staff = (Staff) session.getAttribute("staff");
        //为该笔出库单设置审核状态和审核时间为当前时间
        outlibrarytransfer.setExamstaffid(staff.getStaffid());//审核人员
        outlibrarytransfer.setExamtime(new Date());//审核时间
        outlibrarytransfer.setExamstate(2);//审核状态
        //更改信息
        int i = outlibrarytransferMapper.updateByPrimaryKeySelective(outlibrarytransfer);

        //判断平调后的店铺是否有该商品，有则修改商品数量，没有则添加商品
        //根据cid 查询平调的商品信息 得到商品id
        List<OutlibrarytransferGoods> outlibrarytransferGoods = outlibrarytransferGoodsMapper.getOutTransferGoodsByCid(cid);
        //根据商品id和店铺id查询商品数量
        for (OutlibrarytransferGoods outlibrarytransferGoods1 : outlibrarytransferGoods) {
            Integer goodsid = outlibrarytransferGoods1.getGoodsid();
            Integer branchid = staff.getBranchid();
            BranchGoods branchGoods = branchGoodsMapper.selectByGidAndBid(goodsid,branchid);
            //判断商品是否存在
            if (branchGoods!=null){
                //商品存在，则修改商品数量 用原来的数量加上平调的数量
                Integer counts = branchGoods.getCounts();
                Integer num = outlibrarytransferGoods1.getNum();
                Integer newCounts = counts+num;
                branchGoods.setCounts(newCounts);
                branchGoodsMapper.updateCountsByBranchidAndGoodsid(branchid,goodsid,newCounts);
            }else{
                //商品不存在，则添加商品
                BranchGoods branchGoods1 = new BranchGoods();
                branchGoods1.setBranchid(branchid);
                branchGoods1.setGoodsid(goodsid);
                branchGoods1.setCounts(outlibrarytransferGoods1.getNum());
                int i1 = branchGoodsMapper.insertSelective(branchGoods1);
            }
            //然后修改原来店铺的商品数量
            Integer num1 = outlibrarytransferGoods1.getNum();
            Integer goodsid1 = outlibrarytransferGoods1.getGoodsid();
            BranchGoods branchGoods2 = branchGoodsMapper.selectByGidAndBid(goodsid1,outlibrarytransfer.getBranchid());
            Integer counts1 = branchGoods2.getCounts();
            Integer newCounts1 = counts1-num1;
            branchGoods2.setCounts(newCounts1);
            branchGoodsMapper.updateCountsByBranchidAndGoodsid(outlibrarytransfer.getBranchid(),goodsid1,newCounts1);
            int i2 = branchGoodsMapper.updateByPrimaryKeySelective(branchGoods2);
        }
        return R.ok();
    }

    @Override
    public R reject(OutLibraryVo outLibraryVo) {
        Staff staff = (Staff) session.getAttribute("staff");
        //根据cid查询出库单
        Outlibrarytransfer outlibrarytransfer = outlibrarytransferMapper.selectByPrimaryKey(outLibraryVo.getCid());
        //为该笔出库单设置审核状态和拒绝理由，以及审核时间
        outlibrarytransfer.setExamstaffid(staff.getStaffid());
        outlibrarytransfer.setExamstate(3);
        outlibrarytransfer.setRemarks(outLibraryVo.getRemarks());
        outlibrarytransfer.setExamtime(new Date());
        //更改信息
        int i = outlibrarytransferMapper.updateByPrimaryKeySelective(outlibrarytransfer);
        return R.ok();
    }
}
